﻿package cn.poco.pepsi.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.poco.pepsi.bean.City;
import cn.poco.pepsi.bean.Dish;
import cn.poco.pepsi.bean.Restaurant;

public class CacheDbService {

	private CacheOpenHelper cacheopenhelper;

	public CacheDbService(Context context) {
		cacheopenhelper = new CacheOpenHelper(context);
	}

	public void saveCity(List<City> citys) {
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		db.execSQL("delete from city");
		for (int i = 0; i < citys.size(); i++) {
			db.execSQL("insert into city(city_code,name) values(?,?)", new Object[] { citys.get(i).getKey(),
					citys.get(i).getName() });
		}
		db.close();
	}
	
	public List<City> findCity() {
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from city", null);
		List<City> citys = new ArrayList<City>();
		while (cursor.moveToNext()) {
			City city = new City();
			city.setKey(cursor.getString(cursor.getColumnIndex("city_code")));
			city.setName(cursor.getString(cursor.getColumnIndex("name")));
			citys.add(city);
		}
		cursor.close();
		db.close();
		return citys;
	}
	
	public Integer checkCity() {
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) from city", null);
		cursor.moveToFirst();
		Long count = cursor.getLong(0);
		cursor.close();
		db.close();
		return count.intValue();
	}

	public void saveDishs(ArrayList<Dish> dishs) {
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		for (int i = 0; i < dishs.size(); i++) {
			Dish dish = dishs.get(i);
			db.execSQL("insert into dish(citycode,dishid,dishname) values(?,?,?)", new Object[] { dish.getCitycode(),
					dish.getId(), dish.getName() });
		}
		db.close();
	}
	
	public List<Dish> getDishs(String cityCode){
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		List<Dish> list=new ArrayList<Dish>();
		String sql="select * from dish where citycode=?";
		Cursor cursor=db.rawQuery(sql, new String[]{cityCode});
		while (cursor.moveToNext()) {
			Dish dish=new Dish();
			dish.setCitycode(cursor.getString(cursor.getColumnIndex("citycode")));
			dish.setId(Long.parseLong(cursor.getString(cursor.getColumnIndex("dishid"))));
			dish.setName(cursor.getString(cursor.getColumnIndex("dishname")));
			list.add(dish);
		}
		cursor.close();
		db.close();
		
		return list;
	}

	public List<Dish> finnDish(String citycode) {
		SQLiteDatabase finddb = cacheopenhelper.getReadableDatabase();
		Cursor cursor = finddb.rawQuery("select * from dish where citycode=?", new String[] { citycode });
		List<Dish> dishs = new ArrayList<Dish>();
		while (cursor.moveToNext()) {
			Dish dish = new Dish();
			String code = cursor.getString(cursor.getColumnIndex("citycode"));
			Long dishid = cursor.getLong(cursor.getColumnIndex("dishid"));
			String dishname = cursor.getString(cursor.getColumnIndex("dishname"));
			dish.setCitycode(code);
			dish.setId(dishid);
			dish.setName(dishname);
			dishs.add(dish);
		}
		cursor.close();
		finddb.close();
		return dishs;
	}

	public Integer checkDish(String citycode) {
		SQLiteDatabase finddb = cacheopenhelper.getReadableDatabase();
		Cursor cursor = finddb.rawQuery("select * from dish where citycode=?", new String[] { citycode });
		int count = cursor.getCount();
		cursor.close();
		finddb.close();
		return count;
	}

	public Integer checkReview(String res_code) {
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from t_review where res_code=?", new String[] { res_code });
		int count = cursor.getCount();
		cursor.close();
		db.close();
		return count;
	}

	/**
	 * 保存最近浏览餐厅
	 * 
	 */
	public void savereview(String res_code, String res_title, String res_star, String res_average, String res_dish,
			String res_circle, String res_address, String res_tel, String res_main_dish, String res_comments,
			String res_images, String res_intro, String res_location, String res_join, String res_pepsi,
			String res_discount) {
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		db.execSQL("insert into t_review "
				+ "(res_code,res_title,res_star,res_average,res_dish,res_circle,res_address,res_tel,"
				+ "res_main_dish,res_comments,res_images,res_intro,res_location,res_join,res_pepsi," + "res_discount) "
				+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new Object[] { res_code, res_title, res_star, res_average,
				res_dish, res_circle, res_address, res_tel, res_main_dish, res_comments, res_images, res_intro,
				res_location, res_join, res_pepsi, res_discount });
		db.close();
	}

	/**
	 * 清空最近浏览餐厅
	 */
	public void removeReView(String resId) {
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		db.execSQL("delete from t_review where res_code="+resId);
		db.close();
	}

	public ArrayList<Restaurant> getReviewRestaurant() {
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from t_review order by id desc", null);
		ArrayList<Restaurant> resList = new ArrayList<Restaurant>();
		while (cursor.moveToNext()) {
			Restaurant res = new Restaurant();
			String id = cursor.getString(cursor.getColumnIndex("res_code"));
			if (id != null && !id.equals("")) {
				res.setId(Integer.parseInt(id));
			}
			res.setTitle(cursor.getString(cursor.getColumnIndex("res_title")));
			res.setStar(cursor.getString(cursor.getColumnIndex("res_star")));
			res.setAverage(cursor.getString(cursor.getColumnIndex("res_average")));
			res.setDish(cursor.getString(cursor.getColumnIndex("res_dish")));
			res.setArea(cursor.getString(cursor.getColumnIndex("res_circle")));
			res.setAddress(cursor.getString(cursor.getColumnIndex("res_address")));
			res.setTel(cursor.getString(cursor.getColumnIndex("res_tel")));
			res.setMaindish(cursor.getString(cursor.getColumnIndex("res_main_dish")));
			String comment = cursor.getString(cursor.getColumnIndex("res_comments"));
			if (comment != null && !comment.equals("")) {
				res.setComment(Integer.parseInt(comment));
			}
			String images = cursor.getString(cursor.getColumnIndex("res_images"));
			if (images != null && !comment.equals("")) {
				res.setBlog(Integer.parseInt(images));
			}
			res.setIntro(cursor.getString(cursor.getColumnIndex("res_intro")));
			res.setLocation(cursor.getString(cursor.getColumnIndex("res_location")));
			String vouch = cursor.getString(cursor.getColumnIndex("res_join"));
			if (vouch != null && !vouch.equals("")) {
				res.setVouch(Integer.parseInt(vouch));
			}
			String pepsi = cursor.getString(cursor.getColumnIndex("res_pepsi"));
			if (pepsi != null && !pepsi.equals("")) {
				res.setPepsi(Integer.parseInt(pepsi));
			}
			String discount = cursor.getString(cursor.getColumnIndex("res_discount"));
			if (discount != null && !discount.equals("")&&!discount.equals("0.0")) {
				res.setDiscount(Integer.parseInt(discount));
			}
			res.setVisibility(false);
			resList.add(res);
		}
		cursor.close();
		db.close();
		return resList;
	}

/*	public void saveArea(String citycode, List<Area> areas) {
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		for (int i = 0; i < areas.size(); i++) {
			Area area = areas.get(i);
			db.execSQL("insert into area(area_id,name,city_code) values(?,?,?)", new Object[] { area.getKey(),
					area.getName(), citycode });
		}
		db.close();
	}

	public List<Area> getArea(String citycode) {
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from area where city_code=?", new String[] { citycode });
		List<Area> areas = new ArrayList<Area>();
		while (cursor.moveToNext()) {
			Area area = new Area();
			area.setKey(cursor.getString(cursor.getColumnIndex("area_id")));
			area.setName(cursor.getString(cursor.getColumnIndex("name")));
			areas.add(area);
		}
		cursor.close();
		db.close();
		return areas;
	}

	public Integer checkArea(String citycode) {
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from area where city_code=?", new String[] { citycode });
		int count = cursor.getCount();
		cursor.close();
		db.close();
		return count;
	}

	public String getAreaNameById(String lid) {
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from area where area_id=?", new String[] { lid });
		if (cursor.moveToFirst()) {
			lid = cursor.getString(cursor.getColumnIndex("name"));
		}
		cursor.close();
		db.close();
		return lid;
	}*/

}
